//
//  ViewController.m
//  JJBHourMonitorSDK-demo
//
//  Created by feige on 2023/7/17.
//

#import "ViewController.h"
#import <JJBHourMonitorSDK/JJBHourMonitorSDK.h>

@interface ViewController ()<JJBStudyMonitorSDKDelegate>
@property (weak, nonatomic) IBOutlet UITextView *infoTextView;
@property (weak, nonatomic) IBOutlet UIButton *iniButton;
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;

@property (strong, nonatomic) dispatch_source_t timer;  ///< 模拟录播播放定时器
@property (nonatomic, assign) NSInteger progress;       ///< 录播播放进度
@property (nonatomic, assign) BOOL isPause;             ///< 录播是否暂停
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - Private Mehtod

/// 初始化SDK
- (void)initSDK
{
    JJBDynamicStartSDKModel *startModel = [JJBDynamicStartSDKModel new];
    NSData *jsonData = [self.infoTextView.text dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    NSMutableDictionary *muDic = [[NSMutableDictionary alloc]initWithDictionary:dic];
    startModel.studyInfos = muDic;
    // 初始化SDK并开始学时监管
    [JJBStudyMonitorSDK shared].baseurl = @"https://prodjgpt.jjbedu.cn/smpv2";
    [[JJBStudyMonitorSDK shared] startSDKWithModel:startModel delegate:self];
}

/// 模拟录播开始播放
- (void)simulateDemandBegin
{
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, 0 * NSEC_PER_SEC), 1 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(timer, ^{
        if (!self.isPause) {
            self.progress ++;
            self.progressLabel.text = [NSString stringWithFormat:@"当前模拟进度%lds", self.progress];
            [[JJBStudyMonitorSDK shared] updatePlayTime:self.progress];
        }
    });
    dispatch_resume(timer);
    self.timer = timer;
}

#pragma mark - Response Event

- (IBAction)actions:(UIButton *)sender {
    // 初始化SDK
    if (sender.tag == 0) {
        sender.enabled = NO;
        [self initSDK];
    }
    // 模拟录播开始播放
    else if (sender.tag == 1) {
        [self simulateDemandBegin];
    }
    // 结束
    else if (sender.tag == 2) {
        [[JJBStudyMonitorSDK shared] endStudyMonitor:self.progress];
    }
}

#pragma mark - JJBStudyMonitorSDKDelegate

/**
 * SDK初始化成功
 */
- (void)studyMonitorSDKInitSucceed
{
    NSLog(@"SDK初始化成功");
}

/**
 * 触发了校验
 * @param isCheck 是否需要校验（如果需要校验就暂停视频）
 */
- (void)studyMonitorSDKTriggerValidate:(BOOL)isCheck
{
    NSLog(@"SDK触发了校验：%@", isCheck ? @"需要暂停视频" : @"不需要暂停视频");
    if (isCheck) {
        self.isPause = YES;
    }
}

/**
 * 校验成功，如果在触发校验暂停了视频，在此可以继续播放
 */
- (void)studyMonitorSDKValidateSucceed
{
    NSLog(@"SDK校验成功");
    self.isPause = NO;
}

/**
 * 其他平台重复学习回调
 */
- (void)studyMonitorSDKOtherPlatformStudy
{
    NSLog(@"SDK其他平台重复学习");
}

/**
 * SDK初始化失败
 * @param errCode 错误编码
 * @param errMessage 错误消息
 */
- (void)studyMonitorSDKInitFailure:(NSString *)errCode errMessage:(NSString *)errMessage
{
    NSLog(@"SDK初始化失败 errCode：%@ errMessage：%@", errCode, errMessage);
    [self.iniButton setEnabled:YES];
}

/**
 * 校验失败
 * @param errCode 错误编码
 * @param errMessage 错误消息
 */
- (void)studyMonitorSDKValidateFailure:(NSString *)errCode errMessage:(NSString *)errMessage
{
    NSLog(@"SDK校验失败 errCode：%@ errMessage：%@", errCode, errMessage);
}

@end
